#include <buola/gui.h>

#include <buola/gui/cconfig.h>
#include <buola/os/cicontheme.h>
#include <buola/image/ccolor.h>
#include <buola/app/cregistryfolder.h>

namespace buola { namespace gui {

CConfig gConfig;

void CConfig::Init()
{
    mKeyboard.Init();
    mMouse.Init();
    mIcons.Init();
    mColors.Init();
    mFonts.Init();
}

void CConfig::SIcons::Init()
{
    CRegistryFolder lFolder("/buola/icons");
    std::string lTheme=lFolder.Get<std::string>("theme","oxygen",true);
    mTheme=os::CIconTheme::Get(lTheme);
}

void CConfig::SMouse::Init()
{
    mDoubleClickTime=chrono::milliseconds(300);
    mDoubleClickDistance=4;
    mDragTime=chrono::milliseconds(500);
    mDragDistance=10;
    mWheelScroll=3;
}

void CConfig::SKeyboard::Init()
{
    mBlinkTime=chrono::milliseconds(450);
    mRepeatDelay=chrono::milliseconds(100);
    mRepeatTime=chrono::milliseconds(20);
}

CConfig::SColors::SColors()
{
}

void CConfig::SColors::Init()
{
    CRegistryFolder lFolder("/buola/colors");
    CRegistryMap lMap=lFolder.GetKeyValueMap();

    InitOne(lMap,L"normal_back",img::rgba8(235,235,235,255));
    InitOne(lMap,L"normal_text",img::rgba8(20,20,20,255));
    InitOne(lMap,L"edit_back",img::rgba8(235,235,235,255));
    InitOne(lMap,L"edit_text",img::rgba8(20,20,20,255));
    InitOne(lMap,L"selection_back",img::rgba8(255,192,80,255));
    InitOne(lMap,L"selection_text",img::rgba8(20,20,20,255));
    InitOne(lMap,L"disabled_text",img::rgba8(30,30,100,255));
    InitOne(lMap,L"ruler_back",img::rgba8(170,170,190,255));
    InitOne(lMap,L"progress_fill",img::rgba8(110,144,200,255));
    InitOne(lMap,L"split_back",img::rgba8(120,160,220,255));
    InitOne(lMap,L"split_divider",img::rgba8(110,144,200,255));
    InitOne(lMap,L"property_lines",img::rgba8(190,190,215,255));
    InitOne(lMap,L"liquid_base",img::rgb8(146,148,228)); //violet
//    InitOne(lMap,L"liquid_base",img::rgb8(142,151,174)); //blue
    InitOne(lMap,L"liquid_inactive",img::rgb8(190,190,190)); //violet
//    InitOne(lMap,L"liquid_inactive",img::rgb8(233,233,233)); //violet
    InitOne(lMap,L"liquid_text",img::rgb8(10,10,10)); //violet
    InitOne(lMap,L"micro_close",img::rgb8(200,85,70));
    InitOne(lMap,L"bbar_back",img::rgba8(180,180,180,220));
    InitOne(lMap,L"transparent",img::rgba8(0,0,0,0));
}

void CConfig::SColors::InitOne(const CRegistryMap &pMap,const std::wstring &pName,const img::CColor &pDefault)
{
    img::CColor lColor=pDefault;

    auto lFind=pMap.find(utf8(pName));
    if(lFind!=pMap.end())
        lColor=img::CColor(lFind->second.Get<std::wstring>());

    mAll[pName]=lColor;
}

const img::CColor& CConfig::SColors::Get(const std::wstring &pName)
{
    auto lColor=mAll.find(pName);

    if(lColor==mAll.end())
    {
        throw XNotFound(I18N("Unknown config color ")+utf8(pName));
    }

    return lColor->second;
}

CConfig::SFonts::SFonts()
{
}

void CConfig::SFonts::Init()
{
    ///\todo
//    dbus::CRegistryFolder lFolder("/buola/colors");
//    dbus::CRegistryMap lMap;
//    lFolder.GetKeyValueMap(lMap);
//
//    InitOne(lMap,L"normal_back",RGBA(235,235,235,255));
//    InitOne(lMap,L"normal_text",RGBA(20,20,20,255));
//    InitOne(lMap,L"edit_back",RGBA(235,235,235,255));
//    InitOne(lMap,L"edit_text",RGBA(20,20,20,255));
//    InitOne(lMap,L"selection_back",RGBA(255,192,80,255));
//    InitOne(lMap,L"selection_text",RGBA(20,20,20,255));
//    InitOne(lMap,L"disabled_text",RGBA(30,30,100,255));
//    InitOne(lMap,L"ruler_back",RGBA(170,170,190,255));
//    InitOne(lMap,L"progress_fill",RGBA(110,144,200,255));
//    InitOne(lMap,L"split_back",RGBA(120,160,220,255));
//    InitOne(lMap,L"split_divider",RGBA(110,144,200,255));
//    InitOne(lMap,L"property_lines",RGBA(190,190,215,255));
//    InitOne(lMap,L"liquid_base",RGB(146,148,228)); //violet
////    InitOne(lMap,L"liquid_base",RGB(142,151,174)); //blue
//    InitOne(lMap,L"liquid_inactive",RGB(190,190,190)); //violet
////    InitOne(lMap,L"liquid_inactive",RGB(233,233,233)); //violet
//    InitOne(lMap,L"liquid_text",RGB(10,10,10)); //violet
//    InitOne(lMap,L"micro_close",RGB(200,85,70));
//    InitOne(lMap,L"bbar_back",RGBA(180,180,180,220));
//    InitOne(lMap,L"transparent",RGBA(0,0,0,0));
}

void CConfig::SFonts::InitOne(const CRegistryMap& /*pMap*/,const std::wstring& /*pName*/,unsigned int /*pDefault*/)
{
    ///\todo implement this
//    CColor *lColor=new CColor;
//
//    lColor->SetRGBA(pDefault);
//
//    auto lFind=pMap.find(utf8(pName));
//    if(lFind!=pMap.end())
//        lColor->Set(lFind->second.Get<std::wstring>());
//
//    mAll[pName]=lColor;
}

img::PFont CConfig::SFonts::Get(const std::wstring& /*pName*/)
{
    ///\todo implement this
    return nullptr;
    
//    CColor *lColor=mAll[pName];
//
//    if(!lColor)
//    {
//        throw XNotFound(I18N("Unknown config color ")+utf8(pName));
//    }
//
//    return *lColor;
}

/*namespace gui*/ } /*namespace buola*/ }
